package com.example.kotlinlearn.notification

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat
import com.example.kotlinlearn.ChatActivity
import com.example.kotlinlearn.R
import com.example.kotlinlearn.databinding.ActivityNotificationBinding

class NotificationActivity : AppCompatActivity() {

    lateinit var binding: ActivityNotificationBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_notification)
        binding = ActivityNotificationBinding.inflate(layoutInflater)
        setContentView(binding.root)

        //初始化通知通道
        val manager = getSystemService(Context.NOTIFICATION_SERVICE)as NotificationManager
        //对于android版本大于8的才有下面的代码，所以8版本以上才能接收到以下通知
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            val channel = NotificationChannel("baseNotification","基础通知",NotificationManager
                .IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(channel)
            //重要通知通道
            val channelImportant = NotificationChannel("important","重要通知",NotificationManager
                .IMPORTANCE_HIGH)
            manager.createNotificationChannel(channelImportant)
        }
        //按钮点击发送通知
        binding.button.setOnClickListener {
            val intent = Intent(this, ChatActivity::class.java)
            //PendingIntent相当于延迟执行的intent
            val pi = PendingIntent.getActivity(this,0,intent,0)
            val notification = NotificationCompat.Builder(this,"baseNotification")
                .setContentTitle("bim应用")
                .setContentText("发来一条新消息！")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pi) //设置通知点击事件
                .setLargeIcon(BitmapFactory.decodeResource(resources,R.drawable.icon_left)) //对于大图片使用bitmap
                .setAutoCancel(true)
                .build()
            manager.notify(1,notification)
        }
        binding.button1.setOnClickListener {
            manager.cancel(1)
        }
        binding.buttonText.setOnClickListener {
            val notification = NotificationCompat.Builder(this,"baseNotification")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("重要通知")
                .setStyle(NotificationCompat.BigTextStyle().bigText(
                    "歌曰：“桂棹兮兰桨，击空明兮溯流光。渺渺兮予怀，" +
                        "望美人兮天一方。”客有吹洞箫者，倚歌而和之。其声呜呜然，" +
                        "如怨如慕，如泣如诉，余音袅袅，不绝如缕。舞幽壑之潜蛟，泣孤舟之嫠妇。")
                    .setBigContentTitle("赤壁赋"))
                .setAutoCancel(true)
                .build()
            manager.notify(2,notification)
        }
        binding.buttonImg.setOnClickListener {
            val notification = NotificationCompat.Builder(this,"baseNotification")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("大图片")
                .setStyle(NotificationCompat.BigPictureStyle().bigPicture(
                    BitmapFactory.decodeResource(resources,R.drawable.icon_right)
                ))
                .setAutoCancel(true)
                .build()
            manager.notify(3,notification)
        }
        binding.buttonImportant.setOnClickListener {
            val notification = NotificationCompat.Builder(this,"important")
                .setContentTitle("bim应用")
                .setContentText("发来一条新消息！")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setAutoCancel(true)
                .build()
            manager.notify(4,notification)
        }
    }
}